﻿Imports LSW.Windows.Controls.Forms
Imports System.ComponentModel
Imports LSW.Image

Namespace QQ
    Partial Public Class QQTextBoxEx
        Inherits UserControl
#Region "变量"
        Private BaseText As QQTextBoxBase

        Private _borderImage As Drawing.Image = AssemblyHelper.GetImage("normal.png")
        Private _cursor As Cursor = Cursors.IBeam
        Private _mouseState As EMouseState = EMouseState.Normal
        Private _iconMouseState As EMouseState = EMouseState.Normal
        Private _iconIsButton As Boolean
        Private _icon As Drawing.Image
#End Region

#Region "构造函数"
        ''' <summary>
        ''' 
        ''' </summary>
        Public Sub New()
            Me.SetStyle(ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.DoubleBuffer, True)
            InitializeComponent()
            Me.InitEvents()
            Me.BackColor = Color.Transparent
            Me.UpdateStyles()
        End Sub

#End Region

#Region "自定义事件 && 激发事件的方法"
        ''' <summary>
        ''' 
        ''' </summary>
        Public Event IconClick As EventHandler
        ''' <summary>
        ''' 
        ''' </summary>
        Private Sub OnIconClick()
            RaiseEvent IconClick(Me, EventArgs.Empty)
        End Sub
#End Region

#Region "属性"
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("指定可以在编辑控件中输入的最大字符数。"), Category("行为")> _
        Public Overridable Property MaxLength() As Integer
            Get
                Return Me.BaseText.MaxLength
            End Get
            Set(value As Integer)
                Me.BaseText.MaxLength = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("控件编辑控件的文本是否能够跨越多行。"), Category("行为")> _
        Public Overridable Property Multiline() As Boolean
            Get
                Return Me.BaseText.Multiline
            End Get
            Set(value As Boolean)
                Me.BaseText.Multiline = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("指示将为单行编辑控件的密码输入显示的字符。"), Category("行为")> _
        Public Property IsPasswordChat() As Char
            Get
                Return Me.BaseText.PasswordChar
            End Get
            Set(value As Char)
                Me.BaseText.PasswordChar = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("控制能否更改编辑控件中的文本。"), Category("行为")> _
        Public Overridable Property [ReadOnly]() As Boolean
            Get
                Return Me.BaseText.[ReadOnly]
            End Get
            Set(value As Boolean)
                Me.BaseText.[ReadOnly] = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("指示编辑控件中的文本是否以默认的密码字符显示。"), Category("行为")> _
        Public Overridable Property IsSystemPasswordChar() As Boolean
            Get
                Return Me.BaseText.UseSystemPasswordChar
            End Get
            Set(value As Boolean)
                Me.BaseText.UseSystemPasswordChar = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("指示多行编辑控件是否自动换行。"), Category("行为")> _
        Public Overridable Property WordWrap() As Boolean
            Get
                Return Me.BaseText.WordWrap
            End Get
            Set(value As Boolean)
                Me.BaseText.WordWrap = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("用于显示控件中文本的字体。"), Category("外观")> _
        Public Overridable Shadows Property Font() As Font
            Get
                Return Me.BaseText.Font
            End Get
            Set(value As Font)
                Me.BaseText.Font = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("此组件的前景色，用于显示文本。"), Category("外观")> _
        Public Overridable Shadows Property ForeColor() As Color
            Get
                Return Me.BaseText.ForeColor
            End Get
            Set(value As Color)
                Me.BaseText.ForeColor = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("多行编辑中的文本行，作为字符串值的数组。"), Category("外观")> _
        Public Overridable Property Lines() As String()
            Get
                Return Me.BaseText.Lines
            End Get
            Set(value As String())
                Me.BaseText.Lines = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("指示对于多行编辑控件，将为此控件显示哪些滚动条。"), Category("外观")> _
        Public Overridable Property ScrollBars() As ScrollBars
            Get
                Return Me.BaseText.ScrollBars
            End Get
            Set(value As ScrollBars)
                Me.BaseText.ScrollBars = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("指示应该如何对齐编辑控件的文本。"), Category("外观")> _
        Public Overridable Property TextAlign() As HorizontalAlignment
            Get
                Return Me.BaseText.TextAlign
            End Get
            Set(value As HorizontalAlignment)
                Me.BaseText.TextAlign = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("文本框的图标"), Category("自定义属性")> _
        Public Overridable Property Icon() As Drawing.Image
            Get
                Return Me._icon
            End Get
            Set(value As Drawing.Image)
                Me._icon = value
                MyBase.Invalidate(Me.IconRect)
                Me.PositionTextBox()
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("文本框的图标是否是按钮"), Category("自定义属性")> _
        Public Overridable Property IconIsButton() As Boolean
            Get
                Return Me._iconIsButton
            End Get
            Set(value As Boolean)
                Me._iconIsButton = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("水印文字"), Category("自定义属性")> _
        Public Overridable Property WaterText() As String
            Get
                Return Me.BaseText.WaterText
            End Get
            Set(value As String)
                Me.BaseText.WaterText = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("水印颜色"), Category("自定义属性")> _
        Public Overridable Property WaterColor() As Color
            Get
                Return Me.BaseText.WaterColor
            End Get
            Set(value As Color)
                Me.BaseText.WaterColor = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Browsable(True)> _
        Public Overrides Property Text() As String
            Get
                Return Me.BaseText.Text
            End Get
            Set(value As String)
                Me.BaseText.Text = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        Public Overrides Property Cursor() As Cursor
            Get
                Return Me._cursor
            End Get
            Set(value As Cursor)
                Me._cursor = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        Public Overrides Property MinimumSize() As Size
            Get
                Return New Size(20, 24)
            End Get
            Set(value As Size)
                MyBase.MinimumSize = value
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        Protected Overridable Property MouseState() As EMouseState
            Get
                Return Me._mouseState
            End Get
            Set(value As EMouseState)
                Me._mouseState = value
                MyBase.Invalidate()
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        Protected Overridable Property IconMouseState() As EMouseState
            Get
                Return Me._iconMouseState
            End Get
            Set(value As EMouseState)
                Me._iconMouseState = value
                MyBase.Invalidate()
            End Set
        End Property
        ''' <summary>
        ''' 图标的绘制区域
        ''' </summary>
        Protected Overridable ReadOnly Property IconRect() As Rectangle
            Get
                Return New Rectangle(3, 3, 20, 20)
            End Get
        End Property
#End Region

#Region "方法"
        ''' <summary>
        ''' 加载事件
        ''' </summary>
        Private Sub InitEvents()
            AddHandler Me.BaseText.MouseMove, New MouseEventHandler(AddressOf BaseText_MouseMove)
            AddHandler Me.BaseText.MouseLeave, New EventHandler(AddressOf BaseText_MouseLeave)
            AddHandler Me.BaseText.KeyDown, New KeyEventHandler(AddressOf BaseText_KeyDown)
            AddHandler Me.BaseText.KeyPress, New KeyPressEventHandler(AddressOf BaseText_KeyPress)
            AddHandler Me.BaseText.KeyUp, New KeyEventHandler(AddressOf BaseText_KeyUp)
        End Sub

        ''' <summary>
        ''' 设计界面
        ''' </summary>
        Private Sub InitializeComponent()
            Me.BaseText = New QQTextBoxBase()
            Me.SuspendLayout()
            ' 
            ' BaseText
            ' 
            Me.BaseText.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.BaseText.BorderStyle = System.Windows.Forms.BorderStyle.None
            Me.BaseText.Font = New System.Drawing.Font("微软雅黑", 9.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CByte(134))
            Me.BaseText.Location = New System.Drawing.Point(3, 4)
            Me.BaseText.Margin = New System.Windows.Forms.Padding(0)
            Me.BaseText.Name = "BaseText"
            Me.BaseText.Size = New System.Drawing.Size(172, 18)
            Me.BaseText.TabIndex = 0
            Me.BaseText.WaterColor = System.Drawing.Color.DarkGray
            Me.BaseText.WaterText = ""
            ' 
            ' TextBoxEx
            ' 
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 12.0F)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.BackColor = System.Drawing.Color.Black
            Me.Controls.Add(Me.BaseText)
            Me.Name = "TextBoxEx"
            Me.Size = New System.Drawing.Size(178, 24)
            Me.ResumeLayout(False)
            Me.PerformLayout()

        End Sub

        ''' <summary>
        ''' 偏移文本框
        ''' </summary>
        Protected Overridable Sub PositionTextBox()
            If Me._icon IsNot Nothing Then
                Dim position As Integer = 23
                Me.BaseText.Width -= position
                Me.BaseText.Location = New Point(Me.BaseText.Location.X + position, Me.BaseText.Location.Y)
            End If
        End Sub

#End Region

#Region "事件"
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Protected Sub BaseText_MouseLeave(sender As Object, e As EventArgs)
            Me.MouseState = EMouseState.Leave
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Protected Sub BaseText_MouseMove(sender As Object, e As MouseEventArgs)
            Me.MouseState = EMouseState.Move
        End Sub

        Private Sub BaseText_KeyUp(sender As Object, e As KeyEventArgs)
            Me.OnKeyUp(e)
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub BaseText_KeyPress(sender As Object, e As KeyPressEventArgs)
            Me.OnKeyPress(e)
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub BaseText_KeyDown(sender As Object, e As KeyEventArgs)
            Me.OnKeyDown(e)
        End Sub
#End Region

#Region "Override Methods"
        ''' <summary>
        ''' 当文本框的大小发生改变时，将文本框的类型换成多行文本
        ''' </summary>
        Protected Overrides Sub OnSizeChanged(e As EventArgs)
            MyBase.OnSizeChanged(e)
            If Me.Height > 26 Then
                Me.BaseText.Multiline = True
            Else
                Me.BaseText.Multiline = False
            End If
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            MyBase.OnPaint(e)
            Dim g As Graphics = e.Graphics
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
            Select Case Me._mouseState
                Case EMouseState.Move
                    Using hotLine As Drawing.Image = AssemblyHelper.GetImage("move.png")
                        DrawHelper.RendererBackground(g, Me.ClientRectangle, hotLine, True)
                    End Using
                    Exit Select
                Case Else
                    DrawHelper.RendererBackground(g, Me.ClientRectangle, Me._borderImage, True)
                    Exit Select
            End Select
            If Me._icon IsNot Nothing Then
                Dim iconRect As Rectangle = Me.IconRect
                If Me._iconMouseState = EMouseState.Down Then
                    iconRect.X += 1
                    iconRect.Y += 1
                End If
                g.DrawImage(Me._icon, iconRect, 0, 0, Me._icon.Width, Me._icon.Height, _
                    GraphicsUnit.Pixel)
            End If
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
            MyBase.OnMouseMove(e)
            Me.MouseState = EMouseState.Move
            If Me._icon IsNot Nothing AndAlso Me.IconRect.Contains(e.Location) Then
                If Me._iconIsButton Then
                    Me.Cursor = Cursors.Hand
                Else
                    Me.Cursor = Cursors.[Default]
                End If
            End If
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
            MyBase.OnMouseDown(e)
            If Me._icon IsNot Nothing AndAlso Me._iconIsButton Then
                If e.Button = MouseButtons.Left AndAlso Me.IconRect.Contains(e.Location) Then
                    Me.IconMouseState = EMouseState.Down
                End If
            End If
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
            MyBase.OnMouseUp(e)
            If Me._icon IsNot Nothing AndAlso Me._iconIsButton Then
                Me.IconMouseState = EMouseState.Up
                If e.Button = MouseButtons.Left AndAlso Me.IconRect.Contains(e.Location) Then
                    Me.OnIconClick()
                End If
            End If
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnMouseLeave(e As EventArgs)
            MyBase.OnMouseLeave(e)
            Me.MouseState = EMouseState.Leave
            Me.Cursor = Cursors.[Default]
        End Sub
#End Region
    End Class

End Namespace